package com.wtwd.device.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.wtwd.device.model.vo.PageDataVO;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.entity.Customer;
import com.wtwd.device.service.CustomerService;
import com.wtwd.device.service.DeviceInfoService;
import io.swagger.annotations.*;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zdl
 * @since 2021-08-03
 */
@Api(tags = "客户管理")
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @Resource
    private DeviceInfoService deviceInfoService;

    @ApiOperation(value = "查询客户分页数据", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "name", value = "客户名称", dataType = "String")

    })
    @GetMapping("/page")
    public ResultVO<PageDataVO<Customer>> queryPage(@RequestParam(defaultValue = "1") Integer pageNo
            , @RequestParam(defaultValue = "10") Integer pageSize
            , String name) {
        Page<Customer> page = new Page<>(pageNo, pageSize);
        Customer customer = new Customer();
        customer.setName(name);
        return ResultVO.success(RespCode.SUCCESS, customerService.queryPage(page, customer));
    }

    @ApiOperation("添加客户信息")
    @DynamicParameters(name = "addCustomerModel", properties = {
            @DynamicParameter(name = "name", value = "客户名称", dataTypeClass = String.class, required = true)
    })
    @ApiResponses({@ApiResponse(code = 2002, message = "客户已存在")})
    @PostMapping("/info")
    public ResultVO add(@RequestBody JSONObject addCustomerModel) {
        Customer customer = JSONObject.toJavaObject(addCustomerModel, Customer.class);
        try {
            customerService.save(customer);
        } catch (DuplicateKeyException e) {
            return ResultVO.error(RespCode.CUSTOMER_EXIST);
        }
        return ResultVO.success(RespCode.SUCCESS);
    }

    @ApiOperation("更新客户信息")
    @DynamicParameters(name = "updateCustomerModel", properties = {
            @DynamicParameter(name = "id", value = "客户ID", dataTypeClass = Long.class, required = true)
            , @DynamicParameter(name = "name", value = "客户名称", dataTypeClass = String.class, required = true)
    })
    @ApiResponses({@ApiResponse(code = 2002, message = "客户已存在")})
    @PutMapping("/info")
    public ResultVO update(@RequestBody JSONObject updateCustomerModel) {
        Customer customer = JSONObject.toJavaObject(updateCustomerModel, Customer.class);
        try {
            customerService.update(customer);
        } catch (DuplicateKeyException e) {
            return ResultVO.error(RespCode.CUSTOMER_EXIST);
        }
        return ResultVO.success(RespCode.SUCCESS);
    }

    @ApiOperation("删除客户信息")
    @ApiImplicitParam(name = "id", value = "客户id", required = true, dataTypeClass = Integer.class, paramType = "path")
    @ApiResponses({@ApiResponse(code = 2001, message = "客户已存在设备")})
    @DeleteMapping("/info/{id}")
    public ResultVO delete(@PathVariable Long id) {
        if (deviceInfoService.countDeviceByCustomerId(id) > 0) {
            return ResultVO.error(RespCode.CUSTOMER_EXIST_DEVICE);
        }
        customerService.delete(id);
        return ResultVO.success(RespCode.SUCCESS);
    }

}

